package com.lam.schedule.service.impl;

import java.util.List;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lam.common.utils.StringUtils;
import com.lam.schedule.entity.ScheduleJobLog;
import com.lam.schedule.mapper.ScheduleJobLogMapper;
import com.lam.schedule.param.ScheduleJobLogParam;
import com.lam.schedule.service.ScheduleJobLogService;

/**
 * 定时调度日志Service业务层处理
 * 
 * @author admin
 * @date 2022-06-24
 */
@Service
public class ScheduleJobLogServiceImpl implements ScheduleJobLogService {

	@Autowired
    private ScheduleJobLogMapper scheduleJobLogMapper;

    /**
     * 查询调度日志
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public ScheduleJobLog selectById(String id) {

        return scheduleJobLogMapper.selectById(id);
    }

    /**
     * 查询调度日志列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<ScheduleJobLog> selectList(ScheduleJobLogParam param) {
    
        QueryWrapper<ScheduleJobLog> queryWrapper = new QueryWrapper<>();
        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
        	queryWrapper.and(wr->{
                wr.or().like("job_name", param.getKeywords());
            });
        }
        queryWrapper.eq(Objects.nonNull(param.getScheduleJobId()), "schedule_job_id", param.getScheduleJobId());
        queryWrapper.like(StringUtils.isNotBlank(param.getJobNameKey()), "job_name", param.getJobNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getJobName()), "job_name", param.getJobName());
        queryWrapper.eq(StringUtils.isNotBlank(param.getJobGroup()), "job_group", param.getJobGroup());
        queryWrapper.eq(StringUtils.isNotBlank(param.getExeClass()), "exe_class", param.getExeClass());
        queryWrapper.eq(Objects.nonNull(param.getTriggerType()), "trigger_type", param.getTriggerType());
        queryWrapper.eq(Objects.nonNull(param.getCreateBy()), "create_by", param.getCreateBy());
        
        queryWrapper.le(Objects.nonNull(param.getExeStartTimeBegin()), "exe_start_time", param.getExeStartTimeBegin());
        queryWrapper.ge(Objects.nonNull(param.getExeStartTimeEnd()), "exe_start_time", param.getExeStartTimeEnd());

        queryWrapper.ge(Objects.nonNull(param.getExeCostBegin()), "exe_cost", param.getExeCostBegin());
        queryWrapper.le(Objects.nonNull(param.getExeCostEnd()), "exe_cost", param.getExeCostEnd());
        
        queryWrapper.eq(Objects.nonNull(param.getExeStatus()), "exe_status", param.getExeStatus());

        return scheduleJobLogMapper.selectList(queryWrapper);
    }

    /**
     * 新增调度日志
     * 
     * @param scheduleJobLog 
     * @return 
     */
    @Override
    @Transactional
    public int insert(ScheduleJobLog scheduleJobLog) {
    
        return scheduleJobLogMapper.insert(scheduleJobLog);
    }

    /**
     * 修改调度日志
     * 
     * @param scheduleJobLog 
     * @return 
     */
    @Override
    @Transactional
    public int update(ScheduleJobLog scheduleJobLog) {
    
        return scheduleJobLogMapper.updateById(scheduleJobLog);
    }

    /**
     * 修改定时调度日志执行结果
     * 
     * @param scheduleJobLog 
     * @return 
     */
    @Override
    @Transactional
    public int updateResult(ScheduleJobLog scheduleJobLog) {
    
    	UpdateWrapper<ScheduleJobLog> updateWrapper = new UpdateWrapper<>();
    	updateWrapper.eq("id", scheduleJobLog.getId());
    	updateWrapper.eq("schedule_job_id", scheduleJobLog.getScheduleJobId());

        ScheduleJobLog entity = new ScheduleJobLog();
        entity.setExeEndTime(scheduleJobLog.getExeEndTime());
        entity.setExeCost(scheduleJobLog.getExeCost());
        entity.setExeStatus(scheduleJobLog.getExeStatus());
        entity.setExeMsg(scheduleJobLog.getExeMsg());
        entity.setInstanceIp(scheduleJobLog.getInstanceIp());
        entity.setInstanceId(scheduleJobLog.getInstanceId());
        entity.setInstanceName(scheduleJobLog.getInstanceName());
        entity.setSchedName(scheduleJobLog.getSchedName());
        if(scheduleJobLog.getRemark() != null) {
        	entity.setRemark(scheduleJobLog.getRemark());
        }
		return scheduleJobLogMapper.update(entity , updateWrapper);
    }

    /**
     * 删除调度日志信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    	if(StringUtils.isBlank(idStr)) {
            return 0;
    	}
    	int n = 0;
    	for (String id : StringUtils.str2List(idStr)) {
            n += scheduleJobLogMapper.deleteById(id);
        }
        return n;
    }
}
